
Import player
Import mojo
Import diddy
Import diddy.collections
Import mapNode
Import numberButtons

Import objectSprite 
import statusPanel
Import inputbutton
Import angelfont
Import angelfont.simpleinput


Class MazeScreen Extends Screen
	Const NUMBER_BOX_WIDTH:Int = 200
private
	
	Field player:Player
	
	Field inputNumber:String
	Field currentNumber:Int = -1
	Field mapNode:MapNode
	
	Field objectList:ArrayList<ObjectSprite> = New ArrayList<ObjectSprite>
	Field lastGoodSquare:Int
	Field lastCollidedObject:ObjectSprite
	
	Field lastMouseX:Float
	Field lastMouseY:Float
	Field offsetTweenX:Tween2
	Field offsetTweenY:Tween2
	Field zoomInButton:Button
	Field zoomOutButton:Button
	
	Field numberButton:NumberButtons
	Field statusPanel:StatusPanel
	
'	Field inp:SimpleInput
	
	Field clsTime:int
	Field mapTime:Int
	Field objectTime:Int
	Field numberTime:Int
	Field inputTime:Int
	Field playerTime:Int
	Field buttonTime:Int
	Field counter:int
	Field startTime
	Field isMoving:Bool=false	

	Field offsetX:Float
	Field offsetY:Float

	Field test123:GameImage


public	

	Field screenXMin:Float
	Field screenXMax:Float
	Field screenYMin:Float
	Field screenYMax:Float

	Method New()
		name = "Game"
		
		Local hasNumber:Bool
		Local number:Int = 1
		
		'objectList.RangeChecking(false)

		
	'	inp = New SimpleInput("simple input")
		
		mapNode = New MapNode()
		mapNode.LoadLevel("map1")
		player = New Player()
		'numberButton = New NumberButtons(0,_model.mapHeight+50, SCREEN_WIDTH,150)
		numberButton = New NumberButtons(SCREEN_WIDTH-NUMBER_BOX_WIDTH,0, NUMBER_BOX_WIDTH,SCREEN_HEIGHT)
		statusPanel = new StatusPanel(0,0, SCREEN_WIDTH-NUMBER_BOX_WIDTH, 90)
		
		
		' Disable multiply and divide
		numberButton.buttonDivide.enabled = false
		numberButton.buttonMultiply.enabled = false
		
		screenXMin =Float(-(_model.MAP_WIDTH-SCREEN_WIDTH+NUMBER_BOX_WIDTH))
		screenXMax = 0
		screenYMin = Float(-(_model.MAP_HEIGHT-SCREEN_HEIGHT))
		screenYMax = 0

	'	zoomInButton = Button.Create(game.images.Find("Zoom In"), 25,SCREEN_HEIGHT-25, New ZoomInButton())
	'	zoomOutButton = Button.Create(game.images.Find("Zoom Out"), 70,SCREEN_HEIGHT-25, New ZoomOutButton())
		currentNumber = mapNode.startNumber
	
		moveToSquare(currentNumber, true)
		numberButton.currentNumber = currentNumber
		
		' Create objects based on map square types
		CreateObjects()
		
		offsetTweenX = New Tween2(Tween2.Sine.EaseOut,0,0,3000)
   		offsetTweenY = New Tween2(Tween2.Sine.EaseOut,0,0,3000)

		SetOffset(-mapNode.getTileX(mapNode.endNumber), -mapNode.getTileY(mapNode.endNumber) )



	End

   	Method Start:Void()
		_model.messageWindow.Show("COLLECT "+(_model.maxStars-_model.stars)+" STARS;TO SAVE THE;PRINCESS!", new IntroMessageCallback(self))
		numberButton.currentNumber = currentNumber
  	 End
     
	Method CreateObjects:Void()
		local keyloc:int=-1
	
			For Local ndx:Int=0 Until 144
				
				Local tile:Tile2 = mapNode.tilesMap[ndx]
				Local image:GameImage = null
				
				Local frame:Int = 0
				
				Select tile.type
				Case MapNode.BONUS1_TILE
					image = game.images.Find("selector")
				Case MapNode.END_TILE
					image = game.images.Find("Princess")
				Case MapNode.MULTIPLY_TILE
					image = game.images.Find("Bonus x")
				Case MapNode.DIVIDE_TILE
					image = game.images.Find("Bonus div")
				Case MapNode.STAR_TILE
					image = game.images.Find("Star")
				Case MapNode.COLLECTABLE_TILE1
					image = game.images.Find("Gem Orange")				

				End
				
				If(image<>null) Then
				Print "Adding "+tile.type+" at "+ndx
					Local object:ObjectSprite = new ObjectSprite(image,tile.x,tile.y-91,tile.type,frame)
					object.SetHitBox(0,91,101,80)
					image.MidHandle(false)
					objectList.Add(object)
					object.number = ndx
				Endif
			
			
		Next

	End
	 
	'***************************
	'
	'
   	Method Update:Void()
    
		Local char
		
		If KeyHit(KEY_ESCAPE)

			game.screenFade.Start(50, True)
			game.nextScreen = _model._introScreen
		'	game.nextScreen = _model._bonusScreen				
		End
			
		#Rem If(_model.bonusGameFailed) 'revert to last good square

			'numberButton.Clear
			_model.bonusGameFailed = False ' reset
			_model.messageWindow.Show( "BETTER LUCK NEXT TIME!")
		End
		
		If(_model.bonusGameSuccess) 'Get a star!		
			_model.messageWindow.Show(currentNumber+"YOU NEED TO COLLECT;"+(_model.maxStars-_model.stars)+" STARS")
			_model.bonusGameSuccess=false
		End 
		#End
		
		If (numberButton.answer<>numberButton.NO_ANSWER and isMoving=false) Then
			lastGoodSquare = currentNumber
			moveToSquare(numberButton.answer, false)
			OffsetToNormal()					
		Endif
		
		'
		' Player
		'
		player.OnUpdate
		
		'
		' Number Buttons Box
		'
		numberButton.Update

		'drag scroller
		If(game.TouchDown=false)
			lastMouseX =-1
			lastMouseY= -1
		Endif
		
		If(game.TouchDown() And game.mouseX<SCREEN_WIDTH-numberButton.width And offsetTweenX.isActive=false)
			If(lastMouseX>-1)
				MoveOffset((game.mouseX - lastMouseX),(game.mouseY - lastMouseY))
			Endif

			lastMouseX = game.mouseX
			lastMouseY = game.mouseY
		ElseIf (offsetTweenX<>null)
			If (offsetTweenX.isActive) then
				offsetTweenX.Update()
				offsetTweenY.Update()
				SetOffset(offsetTweenX.Value(), offsetTweenY.Value())
			Endif
		Endif
		
		'only test collisions once per (landed on) square
		If(player.isMoving())
			isMoving=true
		Elseif(isMoving=true)
			isMoving=false
			onStoppedMoving()
		End
		
		_model.messageWindow.Update()
		
		_model.screenX = Clamp(Float(-player.x+offsetX), screenXMin, screenXMax)
		_model.screenY = Clamp(Float(-player.y+offsetY),screenYMin, screenYMax)'nor sure why need the 120?
   End
   
 	'***************************
	'
	'     
   Method OffsetToNormal:Void()
		offsetTweenX.SetValue(offsetX,(SCREEN_WIDTH-NUMBER_BOX_WIDTH)/2)
		offsetTweenY.SetValue(offsetY,SCREEN_HEIGHT2-50)'-50 is so that more of bottom of map shown
		offsetTweenX.Start()
		offsetTweenY.Start()	
   End
   
	'***************************
	'
	'
   Method Render:Void()   
   
	counter+=1
	if(counter=60) then startTime = RealMillisecs 
		'Cls 0,0,0
		if(counter=60) then clsTime = RealMillisecs
			
		SetScissor(0,0,DEVICE_WIDTH-NUMBER_BOX_WIDTH-50,SCREEN_HEIGHT)
		
		
		'
		' TILES
		'
		mapNode.DrawTiles()
		if(counter=60) then 	mapTime =  RealMillisecs
		
		PushMatrix
		Translate(_model.screenX, _model.screenY)
		
		'
		' OBJECTS
		'
		For Local ndx:Int=0 Until objectList.Size
			Local val:ObjectSprite = objectList.Get(ndx)		
			val.Draw(0,0)
		End
		
		'
		' PLAYER
		'
		if(counter=60) then objectTime = RealMillisecs
		player.Draw(-50,-40)
		
		
		if(counter=60) then playerTime = RealMillisecs

		PopMatrix
		
		'
		' NUMBER BUTTONS
		'
		SetScissor(0,0,DEVICE_WIDTH, DEVICE_HEIGHT)
		numberButton.Render	
		
		if(counter=60) then 	numberTime = RealMillisecs
		
	
		'inp.Draw(5,5)
		if(counter=60) then inputTime = RealMillisecs

		'
		'status box
		'

		_model.fpsCounter.Draw(0,100)
		
		if(counter=60) then buttonTime = RealMillisecs
		
		if(counter=60) then
		counter=0
	Endif

	statusPanel.Draw()

	_model.messageWindow.Draw()
	
	DrawText _model.screenX, 10,200
'SetAlpha 0.7
'SetColor 100,100,100
'DrawRect SCREEN_WIDTH2-200,SCREEN_HEIGHT2-100,400, 100
'SetAlpha 1
'SetColor 255,255,255
'			DrawText ("COLLECT "+_model.maxStars+" STARS", SCREEN_WIDTH2, SCREEN_HEIGHT2-50,0.5,0.5)
'			DrawText ("TO SAVE THE PRINCESS!", SCREEN_WIDTH2, SCREEN_HEIGHT2-30,0.5,0.5)
	

'			DrawText(clsTime-startTime, 0 ,100)
'	DrawText( mapTime-clsTime, 0 ,125)
'	DrawText( objectTime-mapTime, 0 ,150)
'	DrawText( playerTime-objectTime, 0 ,175)
'DrawText( 	numberTime-playerTime, 0 ,200)
'DrawText( inputTime-numberTime, 0 ,225)
'DrawText( buttonTime-inputTime, 0 ,250)
'DrawText( SCREENX_RATIO, 0 ,275)
'DrawText( SCREENY_RATIO, 0 ,300)

   End

	Method PostFadeOut:Void()
		Super.PostFadeOut()
	End

   
    Method moveToSquare(inputNumber:Int, startPoint:Bool)
    	Print "Moving from "+currentNumber+" to "+inputNumber
		
		if(inputNumber>100)
			_model.messageWindow.Show("I CAN'T MOVE THERE")
			nextTurnInit(currentNumber)
		elseif(numberButton.currentSign="X")
			if(mapNode.canJumpToSquare(inputNumber))
				player.MoveTo(mapNode.getTileX(inputNumber) ,mapNode.getTileY(inputNumber), 4000)
				numberButton.buttonMultiply.enabled=false
			EndIf
		ElseIf(numberButton.currentSign="/")
			if(mapNode.canJumpToSquare(inputNumber))
				player.MoveTo(mapNode.getTileX(inputNumber) ,mapNode.getTileY(inputNumber), 4000)

				numberButton.buttonDivide.enabled=false
			EndIf
		ElseIf (mapNode.canMoveToSquare(currentNumber, inputNumber)) Then
			If startPoint Then
				player.x = mapNode.getTileX(inputNumber)
				player.y = mapNode.getTileY(inputNumber)
			Else
   				player.MoveTo(mapNode.getTileX(inputNumber) ,mapNode.getTileY(inputNumber), 4000)
			end
		
		Else
			_model.messageWindow.Show("I CAN'T MOVE THERE")
			nextTurnInit(currentNumber)
		End
		
   End
   
	Method onStoppedMoving()		  
		Local callback:WindowCallback = null

		' Has player collided with object?
			For Local ndx:Int=0 Until objectList.Size
				Local val:ObjectSprite = objectList.Get(ndx)
					
				
				' Objects that we DO have to be stopped to activate
				if(player.Collide(val) and player.isMoving=false) Then
					Print "COLLIDED WITH "+val.type
					
					lastCollidedObject = val
					
					
					If (val.type = mapNode.BONUS1_TILE)
						objectList.Remove(val)					
						
						'player.Stop()
						'moveToSquare(val.number, false)
						'remove door from map
						'mapNode.tilesMap[currentNumber].type=OBJ_ID_VOID
						callback=new PreBonusLevelCallback(self)
					ElseIf(val.type = mapNode.MULTIPLY_TILE)
						numberButton.buttonMultiply.enabled=true
						objectList.Remove(val)	
					ElseIf(val.type = mapNode.DIVIDE_TILE)
						numberButton.buttonDivide.enabled=true
						objectList.Remove(val)	
					ElseIf(val.type = mapNode.STAR_TILE)
						_model.stars+=1
						objectList.Remove(val)	
					ElseIf(val.type = mapNode.COLLECTABLE_TILE1)
						_model.collectables+=1
						_model.score+=100
						objectList.Remove(val)	
					ElseIf (val.type = mapNode.END_TILE)
						If(_model.stars>_model.maxStars) Then
							_model.messageWindow.Show("YOU WIN!!!!")
							objectList.Remove(val)	
							'WIN!!
						Else
							'You need to find the key
							_model.messageWindow.Show("YOU NEED TO COLLECT; "+(_model.maxStars-_model.stars)+" STARS")
						EndIf
					EndIf
				
				EndIf
		
			Next
		_model.messageWindow.Show(numberButton.currentNumber+numberButton.currentSign+numberButton.inputNumber + "="+numberButton.answer, callback)
			nextTurnInit(numberButton.answer)
			
		_model.moves+=1
	End

	Method nextTurnInit:void(number:Int)

	  	currentNumber=number
		numberButton.Clear
		numberButton.currentNumber = currentNumber
	End

	Method SetOffset(offset_x:Int, offset_y:Int)
	
		If((-player.x+offset_x) < screenXMin)			
			offsetX=screenXMin+player.x
		ElseIf ((-player.x+offset_x) > screenXMax)
			offsetX=screenXMax+player.x
		Else
			offsetX =offset_x
		Endif

		If((-player.y+offset_y) < screenYMin)			
			offsetY=screenYMin+player.y
		ElseIf ((-player.y+offset_y) > screenYMax)
			offsetY=screenYMax+player.y
		Else
			offsetY =offset_y
		Endif
		
	End

	Method MoveOffset(offset_x:Int, offset_y:Int)
		 SetOffset(offsetX+offset_x, offsetY+offset_y)
	End
	
End


Class IntroMessageCallback Implements WindowCallback
	Field parent:MazeScreen = null

	Method New(parent:MazeScreen)
		self.parent = parent
	End

	Method onClose:Void()
		Print "---"
		Print parent.offsetX
		Print parent.offsetY
		Print "---"
		parent.offsetTweenX = New Tween2(Tween2.Sine.EaseInOut,parent.offsetX,SCREEN_WIDTH2,3000)
		parent.offsetTweenY = New Tween2(Tween2.Sine.EaseInOut,parent.offsetY,SCREEN_HEIGHT2,3000)
		parent.offsetTweenX.Start()
		parent.offsetTweenY.Start()

	End
End

Class PreBonusLevelCallback Implements WindowCallback
	Field parent:MazeScreen = null

	Method New(parent:MazeScreen)
		self.parent = parent
	End

	Method onClose:Void()		
		game.screenFade.Start(50, True)
		local nextBonus:Int = Rnd(3)
		if(nextBonus=0) then		
			_model._bonusShootScreen = New BonusShootScreen()							
			game.nextScreen = _model._bonusShootScreen
		elseif(nextBonus=1) then
			_model._bonusCountingScreen = New BonusCountingScreen()
			game.nextScreen = _model._bonusCountingScreen
		Else
			_model._bonusOddEvensScreen = New BonusOddEvensScreen()
			game.nextScreen = _model._bonusOddEvensScreen
		Endif
	End
End


Class ZoomInButton Extends ButtonEvent
	Method OnClicked:Void()
		_model.zoom+=0.1
	End
End

Class ZoomOutButton Extends ButtonEvent
	
	Method OnClicked:Void()
		_model.zoom-=0.1
	End
End






















